//
//  JYRoot.m
//  s.
//
//  Created by fanshijian on 2022/8/29.
//  Copyright © 2022 J. All rights reserved.
//

#import "JYRoot.h"
#import <CALayer+ZHExtension.h>


@implementation JYRoot

#pragma mark - 逻辑跳转



#pragma mark - Bottom Alert View
+ (JYPopContainerView *)popBottomView:(UIView *)view
                          toSuperView:(UIView *)superView
                            animation:(BOOL)animation
                                maker:(void(^)(MASConstraintMaker *make))makerBlock
                                block:(JYPopVoidBlock)block {
    JYPopContainerView *popView = [[JYPopContainerView alloc] initWithSuperView:superView withView:view];
    if (animation) {
        popView.dismissAnimationBlock = ^(JYPopVoidBlock  _Nonnull handelBlock) {
            [view.layer transitionWithType:@"reveal" subtype:@"fromBottom" duration:0.15 function:kCAMediaTimingFunctionEaseOut];
            view.hidden = YES;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (handelBlock) {
                    handelBlock();
                }
            });
        };
    }
    popView.backViewColor = KLColor(@"#000000");
    popView.backViewAlpha = 0.3;
    popView.isTapBack = YES;
    [popView showBlock:block];

    if (makerBlock) {
        [view mas_makeConstraints:makerBlock];
    }

    if (animation) {
        [view.layer transitionWithType:@"moveIn" subtype:@"fromTop" duration:0.15 function:kCAMediaTimingFunctionEaseOut];
    }
    return popView;
}

+ (JYPopContainerView *)popBottomView:(UIView *)view
                               animation:(BOOL)animation
                                   maker:(void(^)(MASConstraintMaker *make))makerBlock
                                   block:(JYPopVoidBlock)block {
    UIView *window = [UIApplication sharedApplication].keyWindow;
    return [self popBottomView:view toSuperView:window animation:animation maker:makerBlock block:block];
}

+ (void)showMapNavFrom:(CLLocationCoordinate2D)from
                    to:(CLLocationCoordinate2D)to
                 fAddr:(NSString *)fAddr
                 tAddr:(NSString *)tAddr
{
    //    https://api.map.baidu.com/lbsapi/cloud/uri-introios.htm
        klActions(nil, nil, @[@"取消",@"高德地图",@"百度地图"], ^(NSInteger idx) {
            if (idx > 0) {
                CGFloat originLat = from.latitude;
                CGFloat originLng = from.longitude;
                NSString *originName = fAddr;
                
                CGFloat destLat = to.latitude;
                CGFloat destLng = to.longitude;
                NSString *destName = tAddr;
                NSString *urlString = nil;
                if (idx == 1) {
                    // 高德
                    urlString = kStringFormat(@"iosamap://path?sourceApplication=AppName&sid=&slat=%f&slon=%f&sname=%@&did=&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",originLat,originLng,originName,destLat,destLng,destName);
                    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
                        ZHRunEngine(kRunCMDINFO(@"open_extern_url",@{@"url":urlString}), nil);
                    }else {
                        toast(@"请检查您的手机是否安装了高德地图");
                    }
                }else if (idx == 2) {
                    // 百度
                    urlString = kStringFormat(@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=gcj02",originLat,originLng,originName,destLat,destLng,destName);
                    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
                        ZHRunEngine(kRunCMDINFO(@"open_extern_url",@{@"url":urlString}), nil);
                    }else {
                        toast(@"请检查您的手机是否安装了百度地图");
                    }
                }
            }
        });
}
@end
